<?php


define("TH_DBG_ENABLED", TRUE);
define("TH_DBG_ERRLVL", TRUE);
define("TH_DBG_XINIT", TRUE);
define("TH_DBG_ALLOWED", '127.0.0.1 ::1');

include 'lib.dbg.php';
include 'lib.orm.php';
include 'lib.orm.item.php';
include 'lib.orm.query.php';


$dsn = 'mysql:dbname=t3h1337_test;host=127.0.0.1';
$user = 'admin';
$password = '13579';
$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);


try {
    $dbh = new PDO($dsn, $user, $password, $options);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}


class TH_DbTbl_Country extends TH_DbItem {
    protected $behavior = array(
        'b.load' => TH_DbBase::L_REQ
    );

    protected $props = array(
		'id' => array(
			"v.type"   => TH_DbBase::T_INT,
            "v.access" => TH_DbBase::A_READONLY,
//            "v.access" => TH_DbBase::A_FULL,
        ),
		'code' => array(
			"v.type"   => TH_DbBase::T_STRING
        ),
		'name' => array(
			"v.type"   => TH_DbBase::T_STRING
        )
    );

    protected $rels = array(
		'cities_br' => array(
            "r.access" => TH_DbBase::A_FULL,
            "r.path"   => "country2city:city:id",
            'r.key'    => 'id'
        )
        ,
		'cities_pl' => array(
            "r.access" => TH_DbBase::A_FULL,
            "r.path"   => "city:country_id",
            'r.key'    => 'id'
        )
    );


}

class TH_DbTbl_City extends TH_DbItem {
    protected $props = array(
		'id' => array(
			"v.type"   => TH_DbBase::T_INT,
            "v.access" => TH_DbBase::A_READONLY
        ),
		'name' => array(
			"v.type"   => TH_DbBase::T_STRING
        ),
		'country_id' => array(
			"v.type"   => TH_DbBase::T_INT,
            'v.access' => TH_DbBase::A_READONLY,
        )
    );

    protected $rels = array(
		'country_pl' => array(
            "r.access" => TH_DbBase::A_FULL,
            "r.path"   => "country:id",
            'r.key'    => 'country_id'
//            'r.type'   => TH_DbBase::R_BACK
        )
    );

    // TODO: test R_BACK via broker

}

xdbg()->print_status_message();

try {
    $tdb = new TH_DbBase($dbh);

    xdbg()->msg(sizeof($tdb->country));
    xdbg()->msg(sizeof($tdb->city));
    xdbg()->msg($tdb->country[235]->name);
    xdbg()->msg($tdb->country[235]->cities_br);
    xdbg()->msg(sizeof($tdb->country[235]->cities_br));

    for ($i = 0; $i < 3; $i++) {
        xdbg()->msg($tdb->country[235]->cities_br[$i]->name, $tdb->country[235]->cities_br[$i]->id);
    }
    xdbg()->sep();
    xdbg()->msg($tdb->country[235]->name);
    xdbg()->msg($tdb->country[235]->cities_pl);
    xdbg()->msg(sizeof($tdb->country[235]->cities_pl));

    for ($i = 0; $i < 3; $i++) {
        xdbg()->msg($tdb->country[235]->cities_pl[$i]->name, $tdb->country[235]->cities_pl[$i]->id);
    }

    xdbg()->sep();

    xdbg()->msg($tdb->country[235]->cities_br[0]);

    xdbg()->msg($tdb->city[3]->name);
    xdbg()->msg($tdb->city[3]->country_pl[0]->name);

//    xdbg()->msg($tdb->country[235]->id, $tdb->country[235]->code, $tdb->country[235]->name);
//    xdbg()->msg(sizeof($tdb->country[235]->cities_br));
//    xdbg()->msg($tdb->country[235]->cities_br[0]->name);
//    xdbg()->msg(sizeof($tdb->country[235]->cities_br));
//    $tdb->country[235]->id = 222;
//    xdbg()->msg($tdb->country[235]->cities_br[0]->country_pl->clearAll());
//    xdbg()->msg($tdb->country[235]->cities[1]->name);
//    xdbg()->msg($tdb->country[235]->cities[2]->name);

//    xdbg()->msg($tdb->city[0]);
//    xdbg()->msg($tdb->country[0]);

} catch (Exception $e) {
    xdbg()->err($e);
}

?>